/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui;

import com.anhndt.audiobook.business.Resource;
import com.anhndt.audiobook.entity.ItemEntity;
import com.anhndt.audiobook.parser.ItemParser;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.geom.Dimension;

/**
 *
 * @author Sun
 */
public class RateControl extends Component {

    static final int MAX_RATE = 5;
    private int alignment = CENTER;
    private int rate = 3;
    int focusRate;
    private int gap = 1;
    private static Image iconRate, iconUnrate;
    private static int widthIcon;
    private static int heightIcon;

    static {
        if (iconRate == null) {
            iconRate = Util.getImage("rate");
            iconUnrate = Util.getImage("unrate");
            widthIcon = iconRate.getWidth();
            heightIcon = iconRate.getHeight();
        }
    }

    public RateControl() {
        setPreferredSize(new Dimension((widthIcon + gap) * MAX_RATE - gap, heightIcon));
        setSize(getPreferredSize());
        setSelectCommandText(Resource.TXT_RATING);
    }

    public String getUIID() {
        return "Rating";
    }

    protected void focusGained() {
        focusRate = rate;
    }

    public int getFocusRate() {
        return focusRate;
    }
//    protected void focusLost() {
//        super.focusLost();
//    }

    protected boolean isSelectableInteraction() {
        return true;
    }

    public int getAlignment() {
        return alignment;
    }

    public void setAlignment(int align) {
        this.alignment = align;
    }

    public void setRate(int counterRate) {
        if (counterRate <= MAX_RATE && counterRate >= 0) {
            this.rate = counterRate;
        }
        repaint();
    }

    public int getRate() {
        return rate;
    }

    public void incrementRate() {
        int tmp = rate;
        tmp++;
        setRate(tmp);
    }

    public void decrementRate() {
        int tmp = rate;
        tmp--;
        setRate(tmp);
    }

    public void keyPressed(int keyCode) {
        int keyGame = Display.getInstance().getGameAction(keyCode);
        if (keyGame == Display.GAME_LEFT) {
            setHandlesInput(true);
            decrementRate();
        } else if (keyGame == Display.GAME_RIGHT) {
            setHandlesInput(true);
            incrementRate();
        } else {
            setHandlesInput(false);
        }
    }

    public void callServiceRate(final String articleID) {
        new Thread() {

            public void run() {
                ItemParser parser = new ItemParser(Util.userID, Util.passwd, articleID, "" + ItemEntity.TYPE_RATING, null, "" + rate, null, false);
                parser.callservice();
            }
        }.start();
    }

//    protected void paintBackground(Graphics g) {
//        if(hasFocus())
//        super.paintBackground(g);
//    }
////
//    public void paintBackgrounds(Graphics g) {
////        if(hasFocus()){
////            super.paintBackgrounds(g);
////        }
//    }
//
//    protected void paintBorder(Graphics g) {
////        if(hasFocus())
////        super.paintBorder(g);
//    }
    public void paint(Graphics g) {
//        super.paint(g);
        int clipx = g.getClipX();
        int clipy = g.getClipY();
        int clipH = g.getClipHeight();
        int clipW = g.getClipWidth();

        int x = getX();   //default align  = LEFT
        if (alignment == CENTER) {
            x += (getWidth() - (widthIcon + gap) * MAX_RATE) / 2;
        } else if (alignment == RIGHT) {
            x += getWidth() - (widthIcon + gap) * MAX_RATE;
        }
        int y = getY() + (getHeight() - heightIcon) / 2;
        for (int i = 0; i < rate; i++) {
            g.drawImage(iconRate, x + i * (widthIcon + gap), y);
        }
        //draw icon unrate
        for (int i = rate; i < MAX_RATE; i++) {
            g.drawImage(iconUnrate, x + i * (widthIcon + gap), y);
        }
        g.clipRect(clipx, clipy, clipW, clipH);
    }

    public void pointerReleased(int x, int y) {
        super.pointerReleased(x, y);
        int absX = getX();
        if (alignment == CENTER) {
            absX += (getWidth() - (widthIcon + gap) * MAX_RATE) / 2;
        } else if (alignment == RIGHT) {
            absX += getWidth() - (widthIcon + gap) * MAX_RATE;
        }
        int lenght = x - absX;
        if(lenght > getPreferredW())
            lenght = lenght - getPreferredW();
        int percent = lenght * 100 / getPreferredW();  //72 is hard code
        
        int counterStart = Util.divRounded(percent, 20);
        setRate(counterStart);
    }
}
